import Mock from 'mockjs2'
import { builder, getBody } from '../util'

const username = ['admin', 'super']
// 强硬要求 ant.design 相同密码
// '21232f297a57a5a743894a0e4a801fc3',
const password = ['8914de686ab28dc22f30d3d8e107ff6c'] // admin, ant.design

const login = (options) => {
  const body = getBody(options)
  console.log('mock: body', body)
  if (!username.includes(body.username) || !password.includes(body.password)) {
    return builder({ isLogin: true }, '账户或密码错误', 401)
  }

  return builder({
    'id': Mock.mock('@guid'),
    'name': Mock.mock('@name'),
    'username': 'admin',
    'password': '',
    'avatar': 'https://gw.alipayobjects.com/zos/rmsportal/jZUIxmJycoymBprLOUbT.png',
    'status': 1,
    'telephone': '',
    'lastLoginIp': '27.154.74.117',
    'lastLoginTime': 1534837621348,
    'creatorId': 'admin',
    'createTime': 1497160610259,
    'deleted': 0,
    'roleId': 'admin',
    'lang': 'zh-CN',
    'token': '4291d7da9005377ec9aec4a71ea837f',
    'access_token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbImFwcCJdLCJjdXN0b21JbmZvIjoic29tZV9zdHVmZl9oZXJlIiwiZXhwIjoxNTg2ODQ1NzUxLCJhdXRob3JpdGllcyI6WyJwbXM6YnJhbmQ6dXBkYXRlIiwicG1zOnByb2R1Y3Q6dXBkYXRlIiwidW1zOnVtc0FkbWluOmNyZWF0ZSIsInBtczpwcm9kdWN0Q2F0ZWdvcnk6dXBkYXRlIiwidW1zOnVtc0FkbWluOnJlYWQiLCJwbXM6YnJhbmQ6ZGVsZXRlIiwidW1zOnVtc0FkbWluOnVwZGF0ZSIsInBtczpwcm9kdWN0Q2F0ZWdvcnk6Y3JlYXRlIiwicG1zOnByb2R1Y3RBdHRyaWJ1dGU6Y3JlYXRlIiwidW1zOnVtc0FkbWluOmRlbGV0ZSIsInBtczpwcm9kdWN0QXR0cmlidXRlOnVwZGF0ZSIsInBtczpwcm9kdWN0OmNyZWF0ZSIsInBtczpicmFuZDp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VjdDpyZWFkIiwicG1zOnByb2R1Y3Q6ZGVsZXRlIiwicG1zOnByb2R1Y3RBdHRyaWJ1dGU6cmVhZCIsInBtczpwcm9kdWN0Q2F0ZWdvcnk6cmVhZCIsInBtczpicmFuZDpyZWFkIiwicG1zOnByb2R1Y3RBdHRyaWJ1dGU6ZGVsZXRlIiwicG1zOnByb2R1Y3RDYXRlZ29yeTpkZWxldGUiXSwianRpIjoiOThjODU2N2UtYzUwMC00OTAxLWFiZjEtY2EyOTg5YmZmN2JmIiwiY2xpZW50X2lkIjoiY2xpZW50X2FwcCJ9.LKw6ohUdSEdlPBXOFXuu22OSRolkYZ9kSSX1AA7fHvM',
    'expires_in': 863999,
    'scope': 'app',
    'customInfo': 'some_stuff_here',
    'jti': '40c48a14-3648-4ea1-a7a3-5aa0910ef683'
  }, '', 200, { 'Custom-Header': Mock.mock('@guid') })
}

const logout = () => {
  return builder({}, '[测试接口] 注销成功')
}

const smsCaptcha = () => {
  return builder({ captcha: Mock.mock('@integer(10000, 99999)') })
}

const twofactor = () => {
  return builder({ stepCode: Mock.mock('@integer(0, 1)') })
}

Mock.mock(/\unmock\/uaa-api\/oauth\/user\/token/, 'post', login)
Mock.mock(/\/uaa-api\/auth\/logout/, 'post', logout)
Mock.mock(/\/account\/sms/, 'post', smsCaptcha)
Mock.mock(/\/auth\/2step-code/, 'post', twofactor)
